/*
    1. 打开/创建数据库
    2. 打开/创建表
    3. 插入数据
    4. 关闭数据库
*/

#include "sqlite.hpp"
#include <iostream>
#include <cassert>
#include <string>
#include <vector>

// arg用户传递的用于传递额外上下文信息，col_count列数，result[0]第一行的内容，fields_name[0]第一列的名称
int Select_Callback(void *arg, int col_count, char **result, char **fields_name) 
{
    std::vector<std::string> *arry = (std::vector<std::string>*)arg;
    arry->push_back(result[0]);
    return 0; // 必须有！！！
}

int main()
{
    SqliteHelper helper("test.db");
    // 1. 打开/创建数据库
    assert(helper.Open());
    // 2. 打开/创建表
    const char *ct = "create table if not exists student(sn int primary key, name varchar(32), age int);";
    assert(helper.Exec(ct, nullptr, nullptr));
    // 3. 插入数据
    // const char *insert_sql = "insert into student values(1, 'xiao', 3000), (2, 'shenzi', 800), (3, 'ying', 2000);";
    // assert(helper.Exec(insert_sql, nullptr, nullptr));
    // const char *update_sql = "update student set name='魈' where sn=1;";
    // assert(helper.Exec(update_sql, nullptr, nullptr));
    // const char *delete_sql = "delete from student where sn=1;";
    // assert(helper.Exec(delete_sql, nullptr, nullptr));

    std::vector<std::string> arry;
    const char *select_sql = "select name from student;";
    assert(helper.Exec(select_sql, Select_Callback, &arry));
    for(auto &name : arry)
    {
        std::cout << name << std::endl;
    }

    // 4. 关闭数据库
    helper.Close();

    return 0;
}